package com.axialproject.health.exchange.allergytracker;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class DemoServlet extends HttpServlet {

    private static final Logger log = Logger.getLogger(DemoServlet.class.getName());

    public void doPost (HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

        String appUrl = this.getServletContext().getInitParameter("AppUrl");

        try {
            URL url = new URL(appUrl + "/allergytracker/connector");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");

            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
            writer.write("appName=" + URLEncoder.encode("allergytracker", "UTF-8"));
            
            writer.write("&username=" + URLEncoder.encode(req.getParameter("username"), "UTF-8"));
            
            writer.write("&type=" + URLEncoder.encode("allergy", "UTF-8"));
            writer.write("&title=" + URLEncoder.encode("Allergy information from Allergy Tracker", "UTF-8"));

            if (req.getParameter("allergen1") != null && !"".equals(req.getParameter("allergen1"))) {
                writer.write("&allergies=" + URLEncoder.encode(req.getParameter("allergen1") + "|" + req.getParameter("severity1"), "UTF-8"));
            }
            if (req.getParameter("allergen2") != null && !"".equals(req.getParameter("allergen2"))) {
                writer.write("&allergies=" + URLEncoder.encode(req.getParameter("allergen2") + "|" + req.getParameter("severity2"), "UTF-8"));
            }
            if (req.getParameter("allergen3") != null && !"".equals(req.getParameter("allergen3"))) {
                writer.write("&allergies=" + URLEncoder.encode(req.getParameter("allergen3") + "|" + req.getParameter("severity3"), "UTF-8"));
            }
            
            writer.write("&description=" + URLEncoder.encode(req.getParameter("note"), "UTF-8"));
            writer.write("&doctorName=" + URLEncoder.encode("", "UTF-8"));

            writer.close();

            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                resp.setContentType("text/html");
                resp.getWriter().println("Success");
            } else {
                resp.setContentType("text/html");
                resp.getWriter().println("The exchange server returns a bad HTTP code");
            }
        } catch (MalformedURLException e) {
            log.log (Level.SEVERE, e.getMessage(), e);
            resp.setContentType("text/html");
                resp.getWriter().println("Malformed URL");
        } catch (IOException e) {
            log.log (Level.SEVERE, e.getMessage(), e);
            resp.setContentType("text/html");
                resp.getWriter().println("Network error when accessing the exchange");
        }

    }
}
